<?php

class Captcha
{
    
    private $captchastr;
    private $captchaimg;
    
    
    public function __construct($font)
    {
        $strlength = rand(5, 7);
        $this->captchastr = "";

        for($i = 1; $i <= $strlength; $i++)
        {
            $textornumber = rand(1, 4);
            if($textornumber == 1)
            {
                $this->captchastr .= chr(rand(50, 57));  //2-9
            }
            if($textornumber == 2)
            {
                $tmp = chr(rand(65,73));          //A-H, K
                if ($tmp == "I") $tmp = "K";
                $this->captchastr .= $tmp;
            }
            if($textornumber == 3)
            {
                $this->captchastr .= chr(rand(77, 78));  //M-N
            }
            if($textornumber == 4)
            {
                $this->captchastr .= chr(rand(80, 90));  //P-Z
            }
        }
        
        $this->captchaimg = imageCreate(220, 50);

        $backcolor = imageColorAllocate($this->captchaimg, 0, 0, 0); // Black
        $txtcolor = imageColorAllocate($this->captchaimg, 192, 224, 240); //Light blue
        
        // Draw bright pixels above background
        for($i = 1; $i <= 800; $i++)
        {
            imagesetpixel($this->captchaimg, rand(1, 220), rand(1, 50), $txtcolor);
        }
        
        // Draw characters with bright color
        $offsX = (7 - $strlength) * 25 / 2;
        for($i = 1; $i <= $strlength; $i++)
        {
            $clockorcounter = rand(1,2);
            if ($clockorcounter == 1) $rotangle = rand(0, 30);
            if ($clockorcounter == 2) $rotangle = rand(330, 360);
            
            $char = substr($this->captchastr, $i - 1, 1);
            
            imagettftext($this->captchaimg, rand(20,24), $rotangle,($i * 28) - 15 + $offsX, 30, $txtcolor, $font, $char);
        }
        
        // Draw dark pixels above characters
        for($i = 1; $i <= 800; $i++)
        {
            imagesetpixel($this->captchaimg, rand(1, 220), rand(1, 50), $backcolor);
        }
    }
    
    public function getCaptchaImage()
    {
        return $this->captchaimg;
    }

    public function getCaptchaString()
    {
        return $this->captchastr;
    }

}

?>